package com.hrt.test.common;

import com.btcode.log.ILog;
import com.hrt.datasender.datacollect.drive.modbus.tcp.NetBaseDrive;
import com.hrt.datasender.datadao.model.BcDevicesModel;
import com.hrt.datasender.datadao.model.BcSerialModel;
import com.hrt.datasender.mq.MqManager;
import com.hrt.datasender.mq.model.MqModel;
import com.hrt.datasender.mq.model.NetExportDataModel;
import com.hrt.datasender.mq.model.OpcModel;
import com.hrt.datasender.mq.model.PacketModel;
import com.hrt.datasender.mq.model.SerialModel;

/**
 * 消息中间件，用于调度模块、收发模块、数采模块以及UI间的通讯 Created by Chance on 2017/11/1.
 */
public class MqKit {
    private static ILog log;
    /**
     * 中心站消息队列
     * 
     */
    final static String MQ_CENTER_STATION = "MQ_CENTER_STATION";
    /**
     * 数据消息队列
     */
    final static String MQ_BIZ_DATA = "MQ_BIZ_DATA";
    /**
     * 反控消息队列
     */
    final static String MQ_RECRIMINATE = "MQ_RECRIMINATE";
    /**
     * 心跳队列
     */
    final static String MQ_HEATBEAT = "MQ_HEATBEAT";

    public static long getMqMsgSize(String queueName) {
        return 1l;
    }

    public static void consumeMsgFromBizDataMQ(MsgListener msgListener) {
        try {
            MsgQueueManager.getInstance().consume(MQ_BIZ_DATA, new MsgListener() {
                @Override
                public void onReceive(Object msg) {
                    MqModel mqModel = (MqModel) msg;
                    if (mqModel.getMqModelType() == MqModel.MODEL_TYPE_PACKET) {
                        log.info("业务数据队列>>>：" + mqModel);
                        msgListener.onReceive(mqModel);
                    }

                }
            });
        } 
        catch (QueueHasBeenListenedException | NullQueueListenerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void productMsgToHeatBeatMQ(PacketModel packetModel) {
//        log.info(">>>心跳队列：" + packetModel);
        MsgQueueManager.getInstance().produce(MQ_HEATBEAT, packetModel);
    }

    public static void consumeMsgFromHeatBeatMQ(MsgListener msgListener) {
        try {
            MsgQueueManager.getInstance().consume(MQ_HEATBEAT, new MsgListener() {
                @Override
                public void onReceive(Object msg) {
                    MqModel mqModel = (MqModel) msg;
                    log.info("心跳队列>>>：" + mqModel);
                    msgListener.onReceive(mqModel);
                }
            });
        } 
        catch (QueueHasBeenListenedException | NullQueueListenerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 消费串口数据消息
     * 
     * @param msgListener
     */
    public static void consumeMsgFromSerialMQ(BcDevicesModel deviceModel, MsgListener msgListener) {

        String queueName = MqManager.getSerialDataQueueName(deviceModel);

        try {
            MsgQueueManager.getInstance().consume(queueName, new MsgListener() {
                @Override
                public void onReceive(Object msg) {
                    MqModel mqModel = (MqModel) msg;
                    if (mqModel instanceof SerialModel) {
                        log.info("串口数据队列[" + queueName + "]>>>：" + mqModel);
                        msgListener.onReceive(mqModel);
                    }

                }
            });
        } catch (QueueHasBeenListenedException | NullQueueListenerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 发布串口数据消息 队列名称由 <strong>SERIAL_串口号_从站编号</strong> 组成
     * 
     * @param data
     */
    public static void productMsgToSerialMQ(SerialModel data) {
        String queueName = MqManager.getSerialDataQueueName(data);

//        log.info(">>>串口数据队列[" + queueName + "]：" + data);

        MsgQueueManager.getInstance().produce(queueName, data);
    }

    /**
     * 消费网口数据消息
     * 
     * @param msgListener
     */
    public static void consumeMsgFromNetExportMQ(NetBaseDrive deviceModel, MsgListener msgListener) {

        String queueName = MqManager.getNetExportDataQueueName(deviceModel);

        try {
            MsgQueueManager.getInstance().consume(queueName, new MsgListener() {
                @Override
                public void onReceive(Object msg) {
                    MqModel mqModel = (MqModel) msg;
                    if (mqModel instanceof NetExportDataModel) {
                        log.info("网口数据队列[" + queueName + "]>>>：" + mqModel);
                        msgListener.onReceive(mqModel);
                    }

                }
            });
        } catch (QueueHasBeenListenedException | NullQueueListenerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 发布网口数据消息 队列名称由 <strong>SERIAL_串口号_从站编号</strong> 组成
     * 
     * @param data
     */
    public static void productMsgToNetExportMQ(NetExportDataModel data) {
        String queueName = MqManager.getNetExportDataQueueName(data);

//        log.info(">>>网口数据队列[" + queueName + "]：" + data);

        MsgQueueManager.getInstance().produce(queueName, data);
    }

    /**
     * 消费询问串口数据消息
     * 
     * @param msgListener
     */
    public static void consumeMsgFromSerialAskDataMQ(BcSerialModel serialModel, MsgListener msgListener) {

        String queueName = MqManager.getSerialAskDataQueueName(serialModel);

        try {
            MsgQueueManager.getInstance().consume(queueName, new MsgListener() {
                @Override
                public void onReceive(Object msg) {
                    MqModel mqModel = (MqModel) msg;
                    if (mqModel instanceof SerialModel) {
//                        log.info("询问串口数据消息队列>>>：" + mqModel);
                        msgListener.onReceive(mqModel);
                    }

                }
            });
        } 
        catch (QueueHasBeenListenedException | NullQueueListenerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 发布询问串口数据消息 队列名称由 <strong>SERIAL_串口号_从站编号</strong> 组成
     * 
     * @param data
     */
    public static void productAskDataMsgToSerialMQ(SerialModel data) {
//        log.info(">>>询问串口数据消息队列：" + data);

        String queueName = MqManager.getSerialAskDataQueueName(data);

        MsgQueueManager.getInstance().produce(queueName, data);
    }

    /**
     * 消费OPC数据消息
     * 
     * @param msgListener
     */
    public static void consumeMsgFromOpcMQ(BcDevicesModel deviceModel, MsgListener msgListener) {

        String queueName = MqManager.getOpcDataQueueName(deviceModel);

        try {
            MsgQueueManager.getInstance().consume(queueName, new MsgListener() {
                @Override
                public void onReceive(Object msg) {
                    MqModel mqModel = (MqModel) msg;
                    if (mqModel instanceof OpcModel) {
//                        log.info("Opc数据队列[" + queueName + "]>>>：" + mqModel);
                        msgListener.onReceive(mqModel);
                    }

                }
            });
        } 
        catch (QueueHasBeenListenedException | NullQueueListenerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 发布OPC数据消息 队列名称由 <strong>SERIAL_串口号_从站编号</strong> 组成
     * 
     * @param data
     */
    public static void productMsgToOpcMQ(OpcModel data) {
        String queueName = MqManager.getOpcDataQueueName(data);

//        log.info(">>>Opc数据队列[" + queueName + "]：" + data);

        MsgQueueManager.getInstance().produce(queueName, data);
    }

    public abstract static class MsgListener implements IMsgListener {

        public MsgListener() {

        }

        private Object holdObj;

        public MsgListener(Object obj) {
            holdObj = obj;
        }

        public Object getHoldObj() {
            return holdObj;
        }

        public abstract void onReceive(Object msg);
    }

    private static MqModel getOneMsg(String queueName) {
        return null;
    }
}
